package pl.edu.pjwstk.kor.sbql4j.project3;

import pl.wcislo.sbql4j.db4o.Db4oSBQLQuery;
import com.db4o.*;
import com.db4o.ObjectContainer;

import com.db4o.foundation.*;

import com.db4o.internal.*;
import com.db4o.internal.btree.*;

import org.apache.commons.collections.CollectionUtils;

import pl.edu.pjwstk.kor.database.Database;
import pl.edu.pjwstk.kor.database.Sbql4jQuery;
import pl.edu.pjwstk.kor.model.Employee;
import pl.edu.pjwstk.kor.model.Rank;
import pl.edu.pjwstk.kor.model.Service;

import pl.wcislo.sbql4j.exception.*;
import pl.wcislo.sbql4j.java.model.runtime.*;
import pl.wcislo.sbql4j.java.model.runtime.Struct;
import pl.wcislo.sbql4j.java.model.runtime.factory.*;
import pl.wcislo.sbql4j.java.utils.ArrayUtils;
import pl.wcislo.sbql4j.java.utils.OperatorUtils;
import pl.wcislo.sbql4j.java.utils.Pair;
import pl.wcislo.sbql4j.lang.codegen.nostacks.*;
import pl.wcislo.sbql4j.lang.codegen.simple.*;




import pl.wcislo.sbql4j.lang.parser.expression.*;
import pl.wcislo.sbql4j.lang.parser.expression.OrderByParamExpression.SortType;
import pl.wcislo.sbql4j.lang.parser.terminals.*;
import pl.wcislo.sbql4j.lang.parser.terminals.operators.*;
import pl.wcislo.sbql4j.lang.types.*;
import pl.wcislo.sbql4j.lang.xml.*;
import pl.wcislo.sbql4j.model.*;
import pl.wcislo.sbql4j.model.collections.*;
import pl.wcislo.sbql4j.util.*;
import pl.wcislo.sbql4j.util.Utils;
import pl.wcislo.sbql4j.xml.model.*;
import pl.wcislo.sbql4j.xml.parser.store.*;

import java.util.*;
import java.util.Collection;


public class Project3Query_SbqlQuery4Db4o0 implements Db4oSBQLQuery {
    public Project3Query_SbqlQuery4Db4o0() {
    }

    /**
     * query='db.( min(Service.getMinServiceCost()) as minimum,  avg(Service.getMinServiceCost()) as average,  max(Service.getMaxServiceCost()) as maximum)'
    '
     **/
    public pl.wcislo.sbql4j.java.model.runtime.Struct executeQuery(
        final ObjectContainerBase ocb, final Transaction t) {
        final LocalTransaction transLocal = (LocalTransaction) t;
        final java.util.Collection<pl.edu.pjwstk.kor.model.Service> _ident_Service =
            new java.util.ArrayList<pl.edu.pjwstk.kor.model.Service>();
        ClassMetadata _classMeta7 = ocb.classCollection()
                                       .getClassMetadata("pl.edu.pjwstk.kor.model.Service");
        long[] _ids7 = _classMeta7.getIDs(transLocal);

        for (long _id7 : _ids7) {
            LazyObjectReference _ref7 = transLocal.lazyReferenceFor((int) _id7);
            _ident_Service.add((pl.edu.pjwstk.kor.model.Service) _ref7.getObject());
        }

        java.util.Collection<java.lang.Double> _dotResult = new java.util.ArrayList<java.lang.Double>();
        int _dotIndex = 0;

        for (pl.edu.pjwstk.kor.model.Service _dotEl : _ident_Service) {
            if (_dotEl == null) {
                continue;
            }

            if (_dotEl != null) {
                ocb.activate(_dotEl, 1);
            }

            java.lang.Double _mth_getMinServiceCostResult = _dotEl.getMinServiceCost();

            if (_mth_getMinServiceCostResult != null) {
                ocb.activate(_mth_getMinServiceCostResult, 1);
            }

            if (_mth_getMinServiceCostResult != null) {
                ocb.activate(_mth_getMinServiceCostResult, 1);
            }

            _dotResult.add(_mth_getMinServiceCostResult);
            _dotIndex++;
        }

        Number _min0 = null;

        for (Number _minEl0 : _dotResult) {
            _min0 = MathUtils.min(_min0, _minEl0);
        }

        java.lang.Double _minResult = (java.lang.Double) _min0;
        java.lang.Double _asResult_minimum = _minResult;
        final java.util.Collection<pl.edu.pjwstk.kor.model.Service> _ident_Service1 =
            new java.util.ArrayList<pl.edu.pjwstk.kor.model.Service>();
        ClassMetadata _classMeta8 = ocb.classCollection()
                                       .getClassMetadata("pl.edu.pjwstk.kor.model.Service");
        long[] _ids8 = _classMeta8.getIDs(transLocal);

        for (long _id8 : _ids8) {
            LazyObjectReference _ref8 = transLocal.lazyReferenceFor((int) _id8);
            _ident_Service1.add((pl.edu.pjwstk.kor.model.Service) _ref8.getObject());
        }

        java.util.Collection<java.lang.Double> _dotResult1 = new java.util.ArrayList<java.lang.Double>();
        int _dotIndex1 = 0;

        for (pl.edu.pjwstk.kor.model.Service _dotEl1 : _ident_Service1) {
            if (_dotEl1 == null) {
                continue;
            }

            if (_dotEl1 != null) {
                ocb.activate(_dotEl1, 1);
            }

            java.lang.Double _mth_getMinServiceCostResult1 = _dotEl1.getMinServiceCost();

            if (_mth_getMinServiceCostResult1 != null) {
                ocb.activate(_mth_getMinServiceCostResult1, 1);
            }

            if (_mth_getMinServiceCostResult1 != null) {
                ocb.activate(_mth_getMinServiceCostResult1, 1);
            }

            _dotResult1.add(_mth_getMinServiceCostResult1);
            _dotIndex1++;
        }

        java.lang.Double _avgResult = 0d;

        if ((_dotResult1 != null) && !_dotResult1.isEmpty()) {
            Number _avgSum0 = null;

            for (Number _avgEl0 : _dotResult1) {
                _avgSum0 = MathUtils.sum(_avgSum0, _avgEl0);
            }

            _avgResult = _avgSum0.doubleValue() / _dotResult1.size();
        }

        java.lang.Double _asResult_average = _avgResult;
        pl.wcislo.sbql4j.java.model.runtime.Struct _commaResult = OperatorUtils.cartesianProductSS(_asResult_minimum,
                _asResult_average, "minimum", "average");
        final java.util.Collection<pl.edu.pjwstk.kor.model.Service> _ident_Service2 =
            new java.util.ArrayList<pl.edu.pjwstk.kor.model.Service>();
        ClassMetadata _classMeta9 = ocb.classCollection()
                                       .getClassMetadata("pl.edu.pjwstk.kor.model.Service");
        long[] _ids9 = _classMeta9.getIDs(transLocal);

        for (long _id9 : _ids9) {
            LazyObjectReference _ref9 = transLocal.lazyReferenceFor((int) _id9);
            _ident_Service2.add((pl.edu.pjwstk.kor.model.Service) _ref9.getObject());
        }

        java.util.Collection<java.lang.Double> _dotResult2 = new java.util.ArrayList<java.lang.Double>();
        int _dotIndex2 = 0;

        for (pl.edu.pjwstk.kor.model.Service _dotEl2 : _ident_Service2) {
            if (_dotEl2 == null) {
                continue;
            }

            if (_dotEl2 != null) {
                ocb.activate(_dotEl2, 1);
            }

            java.lang.Double _mth_getMaxServiceCostResult = _dotEl2.getMaxServiceCost();

            if (_mth_getMaxServiceCostResult != null) {
                ocb.activate(_mth_getMaxServiceCostResult, 1);
            }

            if (_mth_getMaxServiceCostResult != null) {
                ocb.activate(_mth_getMaxServiceCostResult, 1);
            }

            _dotResult2.add(_mth_getMaxServiceCostResult);
            _dotIndex2++;
        }

        Number _max0 = null;

        for (Number _maxEl0 : _dotResult2) {
            _max0 = MathUtils.max(_max0, _maxEl0);
        }

        java.lang.Double _maxResult = (java.lang.Double) _max0;
        java.lang.Double _asResult_maximum = _maxResult;
        pl.wcislo.sbql4j.java.model.runtime.Struct _commaResult1 = OperatorUtils.cartesianProductSS(_commaResult,
                _asResult_maximum, "", "maximum");
        pl.wcislo.sbql4j.db4o.utils.DerefUtils.activateResult(_commaResult1, ocb);

        return _commaResult1;
    }
}
